package resources.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class UploadAjax
 */
public class UploadAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			PrintWriter out = response.getWriter();
			System.out.println("ABCD");
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			if (ServletFileUpload.isMultipartContent(request)){
				FileItemFactory factory = new DiskFileItemFactory();
				ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
				List fileItemsList = servletFileUpload.parseRequest(request);					
				Iterator it = fileItemsList.iterator();
				FileItem fileItemTemp = null;
				int i = 0;
				while (it.hasNext()){		
					fileItemTemp = (FileItem)it.next();
					if(fileItemTemp==null)return;
					if (fileItemTemp.isFormField()){
						//do nothing
					}else{
						if (fileItemTemp!=null){
							i++;
							int percent = (int)((i*100)/fileItemTemp.getSize());
							System.out.println(percent);
						    String fileName = new Date().getTime() + ".png";
						    if (fileItemTemp.getSize() > 0){					        
//						        String dirName = getServletContext().getRealPath("/upload/");
						    	String dirName = "D:";
						        dirName += File.separator + fileName;						        
						        File saveTo = new File(dirName);							        
								fileItemTemp.write(saveTo);
						    }
						}					
					}
				}	
			}				
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			PrintWriter out = response.getWriter();
			System.out.println("ABC");
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			System.out.println(isMultipart);
			if (ServletFileUpload.isMultipartContent(request)){
				FileItemFactory factory = new DiskFileItemFactory();
				ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
				List fileItemsList = servletFileUpload.parseRequest(request);					
				Iterator it = fileItemsList.iterator();
				FileItem fileItemTemp = null;
				while (it.hasNext()){						
					fileItemTemp = (FileItem)it.next();
					if(fileItemTemp==null)return;
					if (fileItemTemp.isFormField()){
						//do nothing
					}else{
						if (fileItemTemp!=null){
						    String fileName = new Date().getTime() + ".png";
						    if (fileItemTemp.getSize() > 0){					        
//						        String dirName = getServletContext().getRealPath("/upload/");
						    	String dirName = "D:";
						        dirName += File.separator + fileName;						        
						        File saveTo = new File(dirName);							        
								fileItemTemp.write(saveTo);
						    }
						}					
					}
				}	
			}				
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

}
